<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BWInputType
 *
 * @author I062261
 */
class BWInputType extends BWEntity {

    //const InputType_KeyValue = 'key_value';
    //put your code here
    protected $inputMedia = null;
    protected $attributeNode = null;
    protected $mediaManager = null;
    protected $messageManager = null;

    public function __construct($inputMedia = NULL) {
        if ($inputMedia instanceof BWInputMedia) {
            $this->inputMedia = $inputMedia;
            $class = get_class($this);
            if ($class != 'BWInputType' && $this->id == NULL) {
                $attributeNode = $this->inputMedia->getAttribute('inputType');
                if (!$attributeNode instanceof BWInputAttribute) {
                    $attributeNode = $this->inputMedia->addAttribute('inputType', $class);
                }
                $this->attributeNode = $attributeNode;
                $this->id = $this->getId();
            }
        }
    }

    public function getId($data = NULL) {
        if ($this->id != NULL)
            return $this->id;

        if ($this->attributeNode instanceof BWInputAttribute) {
            $this->id = $this->attributeNode->getId();
            return $this->id;
        }
    }

    /**
     * 
     * @param type $data
     * @return BWInputAttribute
     */
    protected function getAttributeNode($data = NULL) {
        if ($this->attributeNode == NULL) {
            $class = get_class($this);
            if ($class != 'BWInputType') {
                $attributeNode = $this->inputMedia->getAttribute('inputType');
                if (!$attributeNode instanceof BWInputAttribute) {
                    $attributeNode = $this->inputMedia->addAttribute('inputType', $class);
                }
                $this->attributeNode = $attributeNode;
            }
        }
        return $this->attributeNode;
    }

    public static function getTypeList($data = NULL) {
        $list = array();
        $list['BWKeyValueType'] = 'KeyValueType';
        $list['BWKeyValueType'] = 'TimeSlotType';
        return $list;
    }

    public function setInputMedia($inputMedia) {
        if ($inputMedia instanceof BWInputMedia)
            $this->inputMedia = $inputMedia;
    }

    /**
     * 
     * @param type $data
     * @return BWMediaManager
     */
    public function getMediaManager($data = NULL) {
        if ($this->mediaManager == NULL) {
            $this->mediaManager = new BWMediaManager($this);
        }
        return $this->mediaManager;
    }

    /**
     * 
     * @param type $data
     * @return BWMessageManager
     */
    public function getMessageManager($data = NULL) {
        if ($this->messageManager == NULL) {
            $this->messageManager = new BWMessageManager($this);
        }
        return $this->messageManager;
    }

    public function populateInputTypeByPostData($postData = NULL, $inputIndex = NULL) {
        //parent::populateInputTypeByPostData($postData);
        //populate inputType media
        //$this->populateMediaForInputType($postData, $inputIndex);
        //populate message for inputType
        $processPost = new BWProcessData();
        $myIndex = 'inputType_' . $this->inputMedia->name;
        if ($inputIndex != NULL)
            $myIndex.="_" . $inputIndex;
        $messageList = $processPost->populateMessageListFromPostData($postData, $myIndex);
        $mediaList = $processPost->populateMediaListFromPostData($postData, $myIndex);

        foreach ($messageList as $name => $message) {
            $this->getMessageManager()->addMessage($message);
            $mediaList2 = $processPost->populateMediaListFromPostData($postData, 'message_' . $name);
            foreach ($mediaList2 as $media) {
                $message->getMediaManager()->addMedia($media);
            }
        }

        foreach ($mediaList as $media) {
            $this->getMediaManager()->addMedia($media);
        }

        return $this;
    }

    public function isAvailable() {
        
    }

    /*
      protected function populateMediaForInputType($postData, $inputIndex = NULL) {
      if (is_array($postData) && $postData != NULL) {
      foreach ($postData as $data) {
      $type = $data['type'];
      $class = $data['class'];
      $name = $data['name'];
      $value = $data['value'];
      $index = isset($data['index']) ? $data['index'] : NULL;
      if ($class == 'media') {
      //save string of media
      $media = NULL;
      $myIndex = 'inputType_' . $this->inputMedia->getName();
      if ($inputIndex != NULL)
      $myIndex .="_" . $inputIndex;
      if ($index == $myIndex) {
      if (property_exists('BWMedia', 'name') && $type == 'string') {
      $media = new BWMedia ();
      $media->name = $name;
      $media->content = $value;
      }
      //save image of media
      if (property_exists('BWMedia', 'name') && $type == 'image') {
      $media = new BWMedia ();
      $media->name = $name;
      $media->type = $type;
      $media->content = 'BDynamicFormModel[' . $type . '_' . $class . '_' . $name . '_' . $index . ']'; //构造image的input的name：BDynamicFormModel[image_media_image_0]
      }
      }
      if ($media instanceof BWMedia) {
      $this->getMediaManager()->addMedia($media);
      }
      }
      }
      }
      }
     * 
     */
}
